﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HZ.BidSystem.Model;
using HZ.Data;
using System.Data;

namespace HZ.BidSystem.DAL
{
    public class DAL_Compete
    {
        public static int CompetePrice(IList<int> bidids, IList<string> encryptprices, int competetime)
        {
            StringBuilder sbsql = new StringBuilder();
            for (int i = 0; i < bidids.Count; i++)
            {
                sbsql.AppendFormat(HZ.Data.DAL_Helper.GetUpdateSql(new Compete() { BidId = bidids[i], CompeteEncryptPrice = encryptprices[i], CompeteTimes = competetime }));
            }
            int result = HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(sbsql.ToString());
            return result;
        }


        public static int DecCompete(int? CompanyId, int projectid, string pwd1, int times)
        {
            IDbHelper idbhelper = DbHelperFactory.Create();
            var dt = idbhelper.ExecuteTable(string.Format("SELECT BidId, CompeteEncryptPrice,CompeteTimes FROM dbo.TB_Compete WHERE BidId IN (SELECT BidId FROM dbo.TB_Project_Bid WHERE CompanyId = {0} AND ProjectId = {1}) AND CompeteTimes = {2}", CompanyId.Value, projectid, times));
            StringBuilder sb = new StringBuilder();
            var ie = HZ.Security.EncryptFactory.Instance;
            try
            {
                //idbhelper.BeginTrans();
                foreach (DataRow item in dt.Rows)
                {
                    if (!string.IsNullOrEmpty(item["CompeteEncryptPrice"].ToString()))
                        idbhelper.ExecuteNonQuery(string.Format("UPDATE dbo.TB_Compete SET CompeteDecPrice = {0} WHERE BidId = {1} AND  CompeteTimes = {2}", ie.DecryptString(item["CompeteEncryptPrice"].ToString(), pwd1), item["BidId"].ToString(), times));

                }
                //idbhelper.CommitTrans();
                return 1;
            }
            catch (Exception ee)
            {
                //idbhelper.RollTrans();
                HZ.Common.log4netManager.LogDebug("解密报价", "解密报价发生异常", ee);

                throw ee;
                return 0;

            }
        }
    }
}
